<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $this->display();
    }

    public function register(){
 	$usermodel=D('users');
   	if($usermodel->create()){
   		if($_FILES['photo']['error']==0){ //判断文件上传是否成功
   		
   		$fileName=$this->img();
   	    $usermodel->photo=$fileName;
   	}
   	   	if($usermodel->add()){
   		$this->success('success',U('Admin/Index/users'));
   	}else{
   		$this->error('failed');
   	}
   }else{
   	$this->error($usermodel->getError());
   }
    }

    public function upload(){
   $upload = new \Think\Upload();// 实例化上传类
   $upload->maxSize = 3145728 ;// 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
//$upload->savePath = ''; // 设置附件上传（子）目录
$upload->autoSub=false;
// 上传文件
$info = $upload->upload();
if(!$info) {// 上传错误提示错误信息
$this->error($upload->getError());
}else{// 上传成功
//$this->success('上传成功！');
	return $info;
}
}
function img(){
	$photoinfo = $this->upload();
	$filePath = './Public/Uploads/photo/';
	$oldFileName =$photoinfo['photo']['savename'];
	$oldFileFullName = $filePath."\\".$oldFileName;
	$newFileName ='thumb_'.$oldFileName;
	$newFileFullName = $filePath."\\".$newFileName;
	$image=new \Think\Image();
	$image->open($oldFileFullName);
	$image->thumb(100,100)->save($newFileFullName);
	return $newFileName;
 }  
}